package com.ivan.toolkit.common;

import java.util.HashMap;
import java.util.concurrent.Executors;

import com.google.common.eventbus.AsyncEventBus;

import lombok.Getter;

public class Events extends HashMap<String, Object> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3919569466040039703L;


	private static AsyncEventBus eventbus = 
			new AsyncEventBus("events", Executors.newSingleThreadExecutor());
	
	public static void register(Object object) {
		eventbus.register(object);
	}

	public static void unregister(Object object) {
		eventbus.unregister(object);
	}
	
	public static void post(Events event) {
		eventbus.post(event);
	}
	
	public static void postSimple(Object key) {
		eventbus.post(new Events(key));
	}
	

	@Getter
	private Object key;
	
	
	public Events(Object key) {
		super();
		this.key = key;
	}

	
	public boolean keyEqualWith(Object value) {
		return key.equals(value);
	}
	
    public <T> T get(String key, Class<T> cls) {
    	try {
    		return cls.cast(this.get(key));
    	} catch (Exception e ){
    		return null;
    	}
    }
    
    public <T> T get(Class<T> cls) {
    	return get(cls.getName(), cls);
    }
	
	public Object put(Object value) {
		return this.put(value.getClass().getName(), value);		
	}

}
